//
//  BuyOrderViewController.m
//  com.modada.new
//
//  Created by ferver7 on 2019/5/9.
//  Copyright © 2019 ferver7. All rights reserved.
//

#import "BuyOrderViewController.h"
#import "SelAddressCell.h"
#import "OrderStoreInfoCell.h"
#import "OrderFirstKindCell.h"
#import "OrderTwoKindCell.h"
#import "OrderConfigCell.h"
#import "OrderPayMoneyCell.h"
#import "StoreInfoItem.h"
#import "TwoSelKindItem.h"
#import "MDDAddressViewController.h"

@interface BuyOrderViewController ()<OrderConfigCellDelegate>
@property (weak, nonatomic) IBOutlet UITableView *selfTableView;
@property (nonatomic,strong) AddressListModel *selModel;
@property (nonatomic,strong) StoreInfoItem *storeItem;
@property (nonatomic,strong) NSArray *cardList;
@property (nonatomic,strong) NSDictionary *selCard;
@end

@implementation BuyOrderViewController
- (IBAction)submitOrder:(id)sender {
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor AxcUI_colorWithHexCode:@"#ebebeb"];
    mNavitationTitle(@"确认XXX订单");
    NSLog(@"到这里来了。 就别走了吧。");
    [self requestAddressList];
    [self loadOrderInfo];
}

#pragma mark -
#pragma mark - **************** tableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4+self.selModuleItemArr.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 2;
    }else if (section == 1){
        return 1;
    }else if (section >= 2 && section<self.selModuleItemArr.count+2){
        NSMutableDictionary * goodInfo = self.selModuleItemArr[section-2];
        NSArray * goodItems = goodInfo[@"list"];
        return goodItems.count+1;
    }
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row == 0 ) {
            UITableViewCell * baseCell = mGetTableViewCell(@"firstLine");
            UIImageView * logo = mViewByTag(baseCell.contentView, 100, UIImageView);
            logo.image = mImageByName(@"收货信息变更");
            
            UILabel * title = mViewByTag(baseCell.contentView, 101, UILabel);
            title.text = @"收货信息";
            return baseCell;
        }else{
            SelAddressCell * sac = mGetTableViewCell(@"selAddressCell");
            if (self.selModel) {
                sac.bgLabel.hidden = YES;
                [sac reloadViewForModel:self.selModel];
            }else{
                sac.bgLabel.hidden = NO;
            }
            return sac;
        }
    }else if (indexPath.section == 1){
        OrderStoreInfoCell * itemCell = mGetTableViewCell(@"orderStoreInfoCell");
        if ([self.storeItem.type isEqualToString:@"1"]) {
            itemCell.stateLabel.text = @"商家";
        }else{
            itemCell.stateLabel.text = @"专营";
        }
        if (self.storeItem.freescope.integerValue == -1) {
            itemCell.mianFeiLabel.text = @"免费配送";
        }else{
            itemCell.mianFeiLabel.text = [NSString stringWithFormat:@"(%@公里内免费配送)",self.storeItem.freescope];
        }
         itemCell.storeNameLabel.text = self.storeItem.storeName;
        return itemCell;
    }else if (indexPath.section >= 2 && indexPath.section<self.selModuleItemArr.count+2){
        if (indexPath.row == 0) {
            OrderFirstKindCell * itemCell = mGetTableViewCell(@"orderFirstKindCell");
            [itemCell.iconImage sd_setImageWithURL:[NSURL URLWithString:self.goodItem.thumbnail] placeholderImage:mImageByName(@"")];
            NSMutableDictionary * goodInfo = self.selModuleItemArr[indexPath.section-2];
            itemCell.kindName.text = goodInfo[@"typeName"];
            return itemCell;
        }else{
            OrderTwoKindCell * itemCell = mGetTableViewCell(@"orderTwoKindCell");
            NSMutableDictionary * goodInfo = self.selModuleItemArr[indexPath.section-2];
            NSArray * goodItems = goodInfo[@"list"];
            TwoSelKindItem * itemObj = goodItems[indexPath.row -1];
            itemCell.xinHaoLabel.text = [NSString stringWithFormat:@"型号:%@",itemObj.attName];
            itemCell.priceLabel.text = [NSString stringWithFormat:@"单价:%@",itemObj.price];
            itemCell.numsLabel.text = [NSString stringWithFormat:@"x%d",itemObj.tapNum];
            return itemCell;
        }
    }else if (indexPath.section == self.selModuleItemArr.count+2){
        OrderConfigCell * itemCell = mGetTableViewCell(@"orderConfigCell");
        itemCell.delegate = self;
        itemCell.path = indexPath;
        CGFloat yunFei = ([self.storeItem.distance integerValue]-[self.storeItem.freescope integerValue])*[self.storeItem.fee floatValue];
        if (yunFei<=0) {
            yunFei=0;
        }
        itemCell.yuFeiLabel.text = [NSString stringWithFormat:@"%.2lf",yunFei];
        if (self.cardList.count == 0) {
            itemCell.youHuiLabel.text = @"暂无优惠券";
        }else if(self.cardList.count > 0 && !self.selCard){
            itemCell.youHuiLabel.text = @"请选择优惠券";
        }
        itemCell.totalMoneyLabel.text = [NSString stringWithFormat:@"¥%.2lf",self.totalMomey];
        return itemCell;
    }else if (indexPath.section == self.selModuleItemArr.count+3){
        OrderPayMoneyCell * itemCell = mGetTableViewCell(@"orderPayMoneyCell");
        return itemCell;
    }
    
    return nil;
}
-(CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0 ) {
            return 44;
        }
        return 80;
    }else if (indexPath.section == 1){
        return 44;
    }else if (indexPath.section >= 2 && indexPath.section<self.selModuleItemArr.count+2){
        if (indexPath.row == 0) {
            return 50;
        }else{
            return 80;
        }
    }else if (indexPath.section == self.selModuleItemArr.count+2){
        return 200;
    }else if (indexPath.section == self.selModuleItemArr.count+3){
        return 150;
    }
    return 40;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 && indexPath.row == 1) {
        MDDAddressViewController * malvc = mGetStoryboardViewController(@"Main", @"mDDAddressViewController");
        WS(weakSelf);
        malvc.returnAddressBlock = ^(AddressListModel *addressModel) {
            weakSelf.selModel = addressModel;
            SelAddressCell * selcell = [tableView cellForRowAtIndexPath:indexPath];
            [selcell reloadViewForModel:addressModel];
            [weakSelf.navigationController  popViewControllerAnimated:YES];
        };
        [self.navigationController pushViewController:malvc animated:YES];
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 1) {
        return 1;
    }
    if (section >= 2 && section<self.selModuleItemArr.count+1){
        return 0.01;
    }
    return 10;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}
#pragma mark - 代理选择运费
-(void)yunfeiAction:(NSIndexPath *)path{
    
    
    
    
}


#pragma mark - **************** 数据请求
-(void)requestAddressList
{
    
    NSMutableDictionary * requestInfo = [[NSMutableDictionary alloc]init];
//    setDickeyobj(requestInfo, @"1", @"add");
//    setDickeyobj(requestInfo, [AppUserBaseModel sharedUserData].uid, @"uid");
    [PPNetworkHelper POST:app_person_address_list parameters:requestInfo success:^(id responseObject) {
        if (YJPPSUCCESS(responseObject)) {
            for (NSDictionary * infoDic in responseObject[@"data"]) {
                AddressListModel * model = [[AddressListModel alloc]init];
                [model setValuesForKeysWithDictionary:infoDic];
                if (model.type.integerValue == 1) {
                    self.selModel = model;
                }
            }
        }else{
            
        }
        [self.selfTableView reloadData];
    } failure:^(NSError *error) {
        
    } showLoading:nil];
    
}
#pragma mark -
#pragma mark - **************** 数据请求
-(void)loadStoreDetail
{
    NSMutableDictionary * parameters = [[NSMutableDictionary alloc]init];
    setDickeyobj(parameters, self.goodItem.storeId, @"storeId");
    setDickeyobj(parameters, @"1", @"add");
    [PPNetworkHelper POST:app_mall_storeInfo parameters:parameters success:^(id responseObject) {
        if (YJPPSUCCESS(responseObject)) {
            self.storeItem = [[StoreInfoItem alloc]init];
            [self.storeItem setValuesForKeysWithDictionary:responseObject[@"data"][@"storeInfo"]];
        }else{
            
        }
        [self.selfTableView reloadData];
    } failure:^(NSError *error) {
        
    } showLoading:nil];
}
#pragma mark -
#pragma mark - **************** 立即购买调取接口
-(void)loadOrderInfo
{
    NSMutableDictionary * parameters = [[NSMutableDictionary alloc]init];
    NSMutableArray * listArr = [[NSMutableArray alloc]init];
    for (NSDictionary * goodInfo in self.selModuleItemArr) {
        NSArray * goodItems = goodInfo[@"list"];
        for (TwoSelKindItem *object in goodItems) {
            NSMutableDictionary * parameters = [[NSMutableDictionary alloc]init];
            setDickeyobj(parameters, object.gaid, @"gaid");
            NSString * selNum = [NSString stringWithFormat:@"%d",object.tapNum];
            setDickeyobj(parameters, selNum, @"quantity");
            [listArr addObject:parameters];
        }
    }
    setDickeyobj(parameters, listArr, @"goods");
    setDickeyobj(parameters, @"-1", @"addressId");
    [PPNetworkHelper POST:app_shoppingCart_shop parameters:parameters success:^(id responseObject) {
        if (YJPPSUCCESS(responseObject)) {
            NSDictionary * infoDic = [responseObject[@"data"][@"storeVOS"] firstObject];
            self.storeItem = [[StoreInfoItem alloc]init];
            [self.storeItem setValuesForKeysWithDictionary:infoDic];
            self.cardList = responseObject[@"data"][@"cardList"];
        }else{
            
        }
        [self.selfTableView reloadData];
    } failure:^(NSError *error) {
        
    } showLoading:nil];
}
@end
